/**
 * Created by tiansha on 2015/10/23.
 */

var scoreConf = [
    {field: 'age', type: 'range', scores: [
        {value: '<18', score: 0},
        {value: '<24', score: -6},
        {value: '<30', score: 5},
        {value: '<50', score: 8},
        {value: '<60', score: 4},
        {value: '>=60', score: 0}]},
    {field: 'education', type: 'value', scores: [
        {value: 'Primary', score: 0},
        {value: 'Junior', score: 3},
        {value: 'Senior', score: 3},
        {value: 'Diploma', score: 5},
        {value: 'College', score: 5},
        {value: 'Master', score: 8}]},
    {field: 'maritalStatus', type: 'value', scores: [
        {value: 'Divorced', score: -9},
        {value: 'Single', score: 5},
        {value: 'Widowed', score: 5},
        {value: 'Married', score: 9}]},
    {field: 'propertyType', type: 'value', scores: [
        {value: 'Rent', score: -5},
        {value: 'DirectRelative', score: 5},
        {value: 'OwnNoCredit', score: 8},
        {value: 'Mortgage', score: 10}]},
    {field: 'companyYears', type: 'range', scores: [
        {value: '<1', score: 0},
        {value: '<3', score: 3},
        {value: '<5', score: 5},
        {value: '>=5', score: 9}]},
    {field: 'companyNature', type: 'value', scores: [
        {value: 'StateOrgans', score: 9},
        {value: 'JointStock', score: 7},
        {value: 'Foreign', score: 7},
        {value: 'PrivateEnterprise', score: 4},
        {value: 'Individual', score: 0},
        {value: 'Freelance', score: -9}]},
    {field: 'position', type: 'value', scores: [
        {value: 'SeniorManager', score: 9},
        {value: 'MiddleManager', score: 6},
        {value: 'GeneralStaff', score: 3}]},
    {field: 'downPercentage', type: 'range', scores: [
        {value: '<0.4', score: 5},
        {value: '<0.5', score: 7},
        {value: '<0.6', score: 8},
        {value: '>=0.6', score: 10}]},
    {field: 'liveYears', type: 'range', scores: [
        {value: '<1', score: 0},
        {value: '<3', score: 5},
        {value: '<5', score: 8},
        {value: '>=5', score: 12}]},
    {field: 'monthLoanPercentage', type: 'range', scores: [
        {value: '<0.1', score: 10},
        {value: '<0.3', score: 8},
        {value: '<0.5', score: 5},
        {value: '>=0.5', score: 0}]},
    {field: 'gender', type: 'value', scores: [
        {value: 'Male', score: 0},
        {value: 'Female', score: 5}]},
    {field: 'creditLevel', type: 'value', scores: [
        {value: 'A', score: 20},
        {value: 'B', score: 10},
        {value: 'C', score: -5},
        {value: 'D', score: -15},
        {value: 'None', score: 5}]}
];

function calculateScore(loanRequest) {
    var totalScore = 0;
    var scoreItem, value;
    for (var i = 0; i < scoreConf.length; i++) {
        scoreItem = scoreConf[i];
        value = loanRequest[scoreItem['field']];
        totalScore += getScore(value, scoreItem.scores, scoreItem.type);
    }
    return totalScore;
}

function getScore(value, scores, type) {
    for (var i = 0; i < scores.length; i++) {
        if (type == 'value') {
            if (scores[i].value == value) {
                return scores[i].score;
            }
        } else if (type == 'range') {
            if (isInRange(value, scores[i].value)) {
                return scores[i].score;
            }
        }
    }
    return 0;
}

function isInRange(value, range) {
    return eval('' + value + range);
}